package com.example.finalhomework_lienyu;//package com.example.finalhomework_lienyu;
//
//import androidx.appcompat.app.AppCompatActivity;
//
//import android.os.Bundle;
//import android.util.Log;
//import android.view.View;
//
//public class TicketActivity extends AppCompatActivity {
//
//        @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_ticket);
//    }
//}

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;


import androidx.appcompat.app.AppCompatActivity;

import com.example.finalhomework_lienyu.util_classes.SQLiteDataBaseHelper;

public class TicketActivity extends AppCompatActivity {
    private EditText editTextTicketDate;
    private RadioGroup radioGroupTicketTime;
    private RadioGroup radioGroupTicketCategory;
    private EditText editTextNumberTicket;
    private TextView textViewTicketsLeft;
    private Button buttonTicketCommit;

    private SQLiteDataBaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket);

        dbHelper = new SQLiteDataBaseHelper(this);

        editTextTicketDate = findViewById(R.id.editText_ticket_Date);
        radioGroupTicketTime = findViewById(R.id.radioGroup_ticket_time);
        radioGroupTicketCategory = findViewById(R.id.radioGroup_ticket_category);
        editTextNumberTicket = findViewById(R.id.editTextNumber_ticket);
        textViewTicketsLeft = findViewById(R.id.textView_tickets_left);
        buttonTicketCommit = findViewById(R.id.button_ticket_commit);

        buttonTicketCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bookTicket();
            }
        });
    }

    private void bookTicket() {
        String date = editTextTicketDate.getText().toString();
        String time = ((RadioButton) findViewById(radioGroupTicketTime.getCheckedRadioButtonId())).getText().toString();
        String category = ((RadioButton) findViewById(radioGroupTicketCategory.getCheckedRadioButtonId())).getText().toString();
        int number = Integer.parseInt(editTextNumberTicket.getText().toString());

        SQLiteDatabase db = dbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(SQLiteDataBaseHelper.COLUMN_DATE, date);
        values.put(SQLiteDataBaseHelper.COLUMN_TIME, time);
        values.put(SQLiteDataBaseHelper.COLUMN_CATEGORY, category);
        values.put(SQLiteDataBaseHelper.COLUMN_NUMBER, number);

        long newRowId = db.insert(SQLiteDataBaseHelper.TABLE_TICKETS, null, values);
        if (newRowId != -1) {
            Toast.makeText(this, "Ticket booked successfully!", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, "Failed to book ticket.", Toast.LENGTH_SHORT).show();
        }
    }
}

